<?php
class Product extends Model {
	public function addProduct($data) {
		return $this->dbo->insertTableRow('product',$data);
	}
	public function getAllProductName() {
		$result = $this->dbo->query("SELECT product.product_id,product.status,product.product_thumb,product.quantity,product.price,product.product_thumb,product.model,product_description.name FROM product INNER JOIN product_description ON product_description.product_id = product.product_id");
		return $result->rows;
	}
	public function getAllProductList($offset,$limit=15){
		$result = $this->dbo->query("SELECT product.product_id,product.status,product.product_thumb,product.quantity,product.price,product.product_thumb,product.model,product_description.name FROM product INNER JOIN product_description ON product_description.product_id = product.product_id LIMIT $offset,$limit");
		return $result->rows;
	}
	public function countProducts(){
		$result = $this->dbo->query("SELECT COUNT(*) as total FROM product");
		return $result->row['total'];
	}
	public function getProductDetailById($product_id){
		$result = $this->dbo->query("SELECT product.*, product_description.* FROM product INNER JOIN product_description ON product.product_id = product_description.product_id WHERE product.product_id = '".(int)$this->dbo->escape($product_id)."'");
		return $result->row;
	}
	public function getProductCategoryByPId($product_id){
		$result = $this->dbo->query("SELECT pc.category_id FROM product p INNER JOIN product_category pc ON p.product_id = pc.product_id WHERE p.product_id = '".(int)$this->dbo->escape($product_id)."'");
		return $result->rows;
	}
	public function getProductAttributeByPId($product_id){
		$result = $this->dbo->query("SELECT pa.attribute_value_id FROM product p INNER JOIN  product_attribute pa ON p.product_id = pa.product_id WHERE p.product_id = '".(int)$this->dbo->escape($product_id)."'");
		return $result->rows;
	}
	public function updateProductById($id, $items){
		return $this->dbo->updateTableRow('product', $id, $items);
	}
	public function delProductById($product_id){
		return $this->dbo->query("DELETE FROM product WHERE product_id = '".(int)$product_id."'");
	}
	public function getNewBestHotProduct(){
		$result = $this->dbo->query("SELECT p.*,pd.* FROM product p INNER JOIN product_description pd ON p.product_id = pd.product_id WHERE p.is_best =1 OR p.is_new = 1 OR p.is_hot = 1");
		return $result->rows;
	}
	public function getProductsByCategoryId($c_id){
		$result = $this->dbo->query("SELECT p.product_id FROM product p INNER JOIN product_category pc ON p.product_id = pc.product_id WHERE pc.category_id='".(int)$c_id."'");
		return $result->rows;
	}
	public function countProductsByCategoryId($c_id){
		$result = $this->dbo->query("SELECT COUNT(*) as total FROM product p INNER JOIN product_category pc ON p.product_id = pc.product_id WHERE pc.category_id='".(int)$c_id."'");
		return $result->row['total'];
	}
	public function getProductInfoInPIds($p_ids,$offset=0,$limit=1){
		$result = $this->dbo->query("SELECT product.*, product_description.* FROM product INNER JOIN product_description ON product.product_id = product_description.product_id WHERE product.product_id IN($p_ids) LIMIT $offset,$limit");
		return $result->rows;
	}
	public function increView($p_id){
		return $this->dbo->query("UPDATE product SET viewed = viewed+1 WHERE product_id = '".(int)$p_id."'");
	}
}
?>